package com.sbs.gaoxinqutoilet.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;

import com.sbs.gaoxinqutoilet.R;
import com.sbs.gaoxinqutoilet.base.BaseBackFragment;
import com.sbs.gaoxinqutoilet.eventbus.EventCenter;
import com.sbs.gaoxinqutoilet.utils.ImageLoaderUtils;
import com.sbs.gaoxinqutoilet.utils.LogUtils;
import com.zhy.autolayout.AutoRelativeLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by 聪明一只哈 on 2018/6/30.
 * Time: 2018/6/30  10:13
 */

public class ImageShowFragment extends BaseBackFragment {

    private static final String IMAGE_URL = "image_url";
    @BindView(R.id.toolbar_back)
    ImageView toolbarBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar_iv_menu)
    ImageView toolbarIvMenu;
    @BindView(R.id.toolbar_tv_menu)
    TextView toolbarTvMenu;
    @BindView(R.id.toolbar_rl_menu)
    AutoRelativeLayout toolbarRlMenu;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.image_show)
    ImageView imageShow;
    Unbinder unbinder;
    private String image_url;

    public static ImageShowFragment newInstance(String imageurl) {

        Bundle args = new Bundle();
        args.putString(IMAGE_URL, imageurl);
        ImageShowFragment fragment = new ImageShowFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_imageshow, container, false);
        unbinder = ButterKnife.bind(this,view);

        initView(inflater);
        return attachToSwipeBack(view);
    }

    /**
     * 初始化toolbar
     * @param inflater
     */
    private void initView(LayoutInflater inflater) {
        EventBus.getDefault().register(this);
        toolbarTitle.setText("图片展示");
        //返回按钮
        toolbarBack.setVisibility(View.VISIBLE);
        //设置回键
        initToolbarBack(toolbar);


        //获取传递过来的值
        Bundle arguments = getArguments();
        if (arguments != null) {
            image_url = arguments.getString(IMAGE_URL);
        }
    }

    /**
     * eventBus接受数据
     *
     * @param center
     */
    @Subscribe
    public void onEventThread(EventCenter center) {

    }

    @Override
    public void onEnterAnimationEnd(Bundle savedInstanceState) {
        super.onEnterAnimationEnd(savedInstanceState);
        showLoadingDialog();
        if (!TextUtils.isEmpty(image_url)&&image_url!=null){
            LogUtils.i("传过来的图像url", "onEnterAnimationEnd: "+image_url);
            ImageLoaderUtils.setImageUrl(_mActivity, image_url, imageShow);

            cancelLoadingDialog();

        }


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
        cancelLoadingDialog();
    }
}
